class Edge implements Comparable<Edge> {
    public final MapNode a;
    public final MapNode b;
    public double weight;
   
    @Override
    public int compareTo(Edge e) {
    	if (weight < e.weight) {
    		return -1;
    	} else if (weight == e.weight) {
    		return 0;
    	} else {
    		return 1;
    	}
    }
    
    public Edge(MapNode argA, MapNode argB, double argWeight){
        a = argA;
        b = argB;
        weight = argWeight;
    }
   
    public boolean spanning(MapNode u, MapNode v) {
        return (u == a && v == b) || (v == a && u == b);
    }
   
    public boolean involves(MapNode u) {
        return u == a || u == b;
    }
   
    public MapNode other(MapNode u) {
        if (u == a) {
                return b;
        } else if (u == b) {
                return a;
        } else {
                throw new IllegalArgumentException("Can't get other vertex when the argument is not on the edge!");
        }
    }
   
    public boolean on(int y, int x) {
        double direction = Math.atan2(b.y-a.y, b.x-a.x);
        double distance = Math.sqrt(Math.pow(b.x-a.x, 2) + Math.pow(b.y-a.y, 2))*24;
       
       
        for (int i = 0; i < distance; i++) {
                double check_x = a.x*24+12 + i * Math.cos(direction);
                double check_y = a.y*24+12 + i * Math.sin(direction);
                int r_x = x*24;
                int r_y = y*24;
               
                /*
                 * Kollar diagonalerna en pixel åt båda håll så att linjer
                 * inte kan passera rätt igenom ett hörn mellan två tiles.
                 */
                if (insideSquare(check_x, check_y, r_x, r_y, 24) ||
                                insideSquare(check_x+1, check_y-1, r_x, r_y, 24) ||
                                insideSquare(check_x-1, check_y+1, r_x, r_y, 24) ||
                                insideSquare(check_x+1, check_y+1, r_x, r_y, 24) ||
                                insideSquare(check_x-1, check_y-1, r_x, r_y, 24)) {
                        return true;
                }
               
        }
       
        return false;
    }
   
    private boolean insideSquare(double x, double y, int o_x, int o_y, int size) {
        return x > o_x && x < o_x+size && y > o_y && y < o_y+size;
    }
   
}
